<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Wallet extends Model
{
    protected $table = 'wallet';
    protected $casts = [
        'tmc' => 'float',
    ];
    protected $fillable = [
        'money','shop_money', 'tmc', 'computing_power',
    ];
    const newbie_task_computing_power = 10; //新手任务完成时获得的算力值

    protected static function update_power($user_id, $power_num, $status)
    {
        switch ($status) {
            case '1': //增加
                DB::table('wallet')->where('user_id',$user_id)->increment('computing_power', $power_num);
                break;
            case '2':
                DB::table('wallet')->where('user_id',$user_id)->decrement('computing_power', $power_num);
                break;
            default:

                break;
        }
    }
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function hasUser($user_id)
    {
        $count = $this->where('user_id', $user_id)->count();
        return $count ? 1 : 0;
    }

    public function get_user_power($user_id)
    {
        $power = $this->where('user_id', $user_id)->first();
        if ($power) {
            return $power->computing_power;
        } else {
            return "";
        }
    }

}
